import utils from "@/utils/util";

// 右侧表格面板放大
export function setScaleUp($el) {
  // 设置遮罩层
  const modalEle = document.createElement("div");
  modalEle.id = "table_scale_up_modal";
  modalEle.style.position = "fixed";
  modalEle.style.left = "0px";
  modalEle.style.top = "0px";
  modalEle.style.width = "100%";
  modalEle.style.height = "100%";
  modalEle.style.backgroundColor = "rgba(0, 0, 0, 0.5)";
  modalEle.style.zIndex = "3000";

  document.body.appendChild(modalEle);

  // 设置父级的层级
  $el.parentElement.style.position = "relative";
  $el.parentElement.style.zIndex = "3001";

  // 设置右边面板
  const rightPanel = $el.querySelector(".right");
  const { top } = rightPanel.getBoundingClientRect();

  const rightPanelScaleUpHeight = 90; //放大后，表格的高度，单位为vh

  rightPanel.style.height = `${rightPanelScaleUpHeight}vh`;
  rightPanel.style.transform = `translateY(-${
    top -
    (((100 - rightPanelScaleUpHeight) / 100) * window.innerHeight +
      utils.pxToFixPx(84)) /
      2
  }px)`;
  rightPanel.style.boxShadow = "0 2px 12px 0 rgba(0, 0, 0, 0.1)";

  // 设置容器，禁止滚动
  const appMain = document.getElementById("app-main");
  appMain.style.setProperty("overflow", "hidden", "important");

  return modalEle;
}

// 右侧表格面板恢复
export function setScaleDown($el, modalEle) {
  document.body.removeChild(modalEle);
  // 设置父级的层级
  $el.parentElement.style.position = "";
  $el.parentElement.style.zIndex = "";

  // 设置右边面板
  const rightPanel = $el.querySelector(".right");
  rightPanel.style.height = "";
  rightPanel.style.transform = "";
  rightPanel.style.boxShadow = "";

  // 设置容器
  const appMain = document.getElementById("app-main");
  appMain.style.overflow = "";
}

// 判断趋势图中都没有值
export function isNoValues(seriesData) {
  let flag = true;
  const index = seriesData.findIndex(
    (e) => ![undefined, null].includes(e.value)
  );
  if (index != -1) {
    flag = false;
  }
  return flag;
}

// 查找series数据中，最后一个有值的对象
export function getLastSerieData(seriesData) {
  let obj = null;
  const len = seriesData.length - 1;
  for (let i = len; i >= 0; i--) {
    const ele = seriesData[i];
    if (![undefined, null].includes(ele.value)) {
      obj = { ...ele };
      break;
    }
  }
  return obj;
}
